<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
</head>

<body>
  <div class="layui-container-fluid">
    <div class="nav">
      <span class="layui-breadcrumb">
        <a href="/dashBoard">首页</a>
        <a href="/cateList">分类管理</a>
        <a><cite>编辑分类</cite></a>
      </span>
    </div>
    <form class="layui-form" action="" style="margin-top: 40px ;max-width: 800px;">
      <div class="layui-form-item">
        <label class="layui-form-label">分类名称</label>
        <div class="layui-input-block">
            <input type="hidden" value="<%= cate._id %>" name="id" >
          <input type="text"
          value="<%= cate.cateName%>"
           name="cateName" required lay-verify="required" placeholder="请输入标题" autocomplete="off"
            class="layui-input">
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">父级分类</label>
        <div class="layui-input-block">
          <select name="pid" lay-verify="required">
            <option>请选择</option>
            <option value="0" <%= cate.pid=="0" && 'selected'%>>顶层分类</option>
            <% for(var i=0;i<cates.length;i++){ %>
              <option <%= cate.pid ==cates[i]._id && 'selected'%> value="<%= cates[i]._id %>">
                <%= cates[i].cateName %>
              </option>
              <% } %>
          </select>
        </div>
      </div>


      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">分类描述</label>
        <div class="layui-input-block">
          <textarea name="desc" placeholder="请输入内容" class="layui-textarea"><%= cate.Desc%>
          </textarea>
        </div>
      </div>
      <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">上传图标</label>
        <div class="layui-input-block">
          <button type="button" class="layui-btn" id="file">
            <i class="layui-icon">&#xe67c;</i>上传图片
          </button>
          <img src="<%= cate.cateIcon || 'https://img2.baidu.com/it/u=26528730,97133990&fm=15&fmt=auto&gp=0.jpg'%>" id="img" width="100" alt="">
        </div>
      </div>


      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>
    <script src="/static/lib/layui/layui.js"></script>
    <script>
      layui.use(['form', 'jquery', 'layer', 'upload'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;
        var upload = layui.upload;
        var imgUrl='<%= cate.cateIcon %>'    //数据为字符串
        //上传图片
        var uploadInst = upload.render({
          elem: '#file' //绑定元素
          , url: '/upload/' //上传接口
          ,field:'file'
          , done: function (res) {
            //上传完毕回调
            $('#img').attr('src',res.data.url)
            imgUrl=res.data.url

          }
          , error: function () {
            //请求异常回调
            console.log('上传失败了')
          }
        });
  

      //监听提交
      form.on('submit(formDemo)', function (data) {
        console.log(data.field)
        data.field.cateIcon=imgUrl
        $.ajax({
          url: "/cateUpdate",
          type: "POST",
          dataType: "json",
          data: data.field,
          success: function (res) {
            if (res.code === 0) {
              layer.msg(res.msg, {
                icon: 1,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
              }, function () {
                //do something
               location.href='/cateList'
              });
            } else {
              layer.msg(res.msg, {
                icon: 2,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
              }, function () {
                //do something
                location.href='/cateList'
              });
            }
          }
        })
        return false;
      });
      });
    </script>
</body>

</html>